import api from "@/api/api";
import {
  CUSTOMER_INFO,
  MERCHANT_INFO,
  USER_SPECICAL_INFO,
  USER_INFO,
} from "@/utils/constant";
import tip from "@/utils/tip";
// 查询用户订阅状态
export const getSubscribeStatus = async (activitySubscribeId) => {
  let result;
  try {
    let customerInfo = uni.getStorageSync(CUSTOMER_INFO);
    const res = await api.getweixinSubscribe({
      method: 'POST',
      header: {
        'jwt': customerInfo.jwt
      },
      query: {
        "name": "活动模版消息",
        "appId": getApp().globalData.app_id,
        "openId": customerInfo.openId,
        "params": {
          "activitySubscribeId": activitySubscribeId,
        }
      }
    });
    if (res.code == 1000) { //已订阅
      result = true
    } else if (res.code == -1010) { //未订阅
      result = false
    } else {
      // tip.plainText(res.msg)
    }
  } catch (error) {}
  return result
};

/*
  订阅
  通过活动id
  获取活动消息模板
 */
export const getActivitySubscribeTemplate = async (activityId) => {
  let result;
  try {
    let customerInfo = uni.getStorageSync(CUSTOMER_INFO)
    const res = await api.queryActivitySubscribeDetail({
      header: {
        'jwt': customerInfo.jwt
      },
    }, activityId);
    if (res.code === 1000) {
      result = res.data
    }
  } catch (error) {}
  return result
};

// 订阅
export const doSubscribe = async (activitySubscribeId) => {
  let result;
  try {
    let customerInfo = uni.getStorageSync(CUSTOMER_INFO);
    const res = await api.weixinSubscribe({
      method: 'POST',
      header: {
        'jwt': customerInfo.jwt
      },
      query: {
        "name": "活动模版消息",
        "type": "活动消息",
        "appId": getApp().globalData.app_id,
        "openId": customerInfo.openId,
        "params": {
          "activitySubscribeId": activitySubscribeId,
        }
      }
    });

    if (res.code == 1000) {
      result = true
    }
  } catch (error) {}
  return result
};

// 心愿单和意向单
export const setFav = async (data, operateType, valueId, type, operateMode) => {
  let result = {};
  try {
    const customerInfo = uni.getStorageSync(CUSTOMER_INFO);
    const merchantInfo = uni.getStorageSync(MERCHANT_INFO)
    const res = await api.setFav({
      method: "POST",
      header: {
        jwt: customerInfo.jwt,
      },
      query: {
        operateType: operateType,
        referenceId: data.outerItemId || data.referenceId,
        referenceName: data.itemName || data.referenceName,
        storeId: merchantInfo.cloudMerchantStoreId,
        type: type,
        extendedField1: data.itemId || data.extendedField1,
        extendedField2: valueId || data.extendedField2,
        operateMode: operateMode || "manual"
      },
    });
    if (res.code === 1000) {
      result.status = true
      result.operateType = operateType
      result.times = res.data.times
    } else {
      result.status = false
    }
  } catch (error) {
    // console.error("updatePolicyStatus error:", error);
  }
  return result
};
// 海报太阳码scene转格式
export const changeToObj = (url) => {
  let params = {};
  const itemArr = url?.split('&');
  itemArr.forEach(it => {
    if (it) {
      const itSplitIndex = it.indexOf('=');
      let key = it.substring(0, itSplitIndex)
      let value = it.substring(itSplitIndex + 1, it.length)
      params[key] = value;
    }
  })
  return params
};

// 到货提醒订阅状态
export const getArrivalSubStatus = async (skuCode) => {
  let result;
  try {
    let customerInfo = uni.getStorageSync(CUSTOMER_INFO)
    let res = await api.queryArrivalResult({
      method: "GET",
      header: {
        jwt: customerInfo.jwt,
      },
      query: {
        skuCode: skuCode,
      },
    })
    if (res.code === 200) {
      result = true // 已设置提醒
    } else {
      result = false //到货提醒
    }
  } catch (error) {}
  return result
};

